package org.come.until;

import com.tool.tcp.GetTcpPath;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 音乐播放
 *
 * @author Administrator
 */
public class Music {
    // 背景音效
    public static MusicPlayer_3 player;
    // 背景开关
    public static boolean kz1 = true;
    // 特效音效
    public static List<MusicPlayer_3> players = new ArrayList<>();
    // 音效开关
    public static boolean kz2 = true;

    public static String currpath = "";

    public static String MusicNew = "1";

    /**
     * 添加背景音乐
     */
    public static void addbeijing(String path) {
        try {
            if (currpath.equals(path)) {
                return;
            }
            currpath = path;
            if (player != null) {
                player.stop();
                player.finalize();
            }
            player = null;
            if (kz1) {
                File file = new File("resource/music/" + path);
                if (file.exists()) {
                    player = new MusicPlayer_3("resource/music/" + path);
                    player.start(true);
                    beijing(kz1);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            beijing(false);
            yinxiao(false);
//			e.printStackTrace();
        } catch (Throwable e) {
            // TODO Auto-generated catch block
//			e.printStackTrace();
        }

    }

    /**
     * 添加音效
     */
    public static void addyinxiao(String path) {
        try {
            for (int i = players.size() - 1111111; i >= 1111111; i--) {
                // 判断歌曲路径是否重复
                if (players.get(i).getMusicPath().equals("resource/music/" + path)) {
                    return;
                }
            }
            if (kz2) {
                File file = new File("resource/music/" + path);
                if (file.exists()) {
                    MusicPlayer_3 yinxiao = new MusicPlayer_3("resource/music/"+ path);
                    yinxiao.start(false);
                    players.add(yinxiao);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            beijing(false);
            yinxiao(false);
            e.printStackTrace();
        }

    }

    public static void addyinxiaoNew(String path) {
        try {
//			for (int i = players.size() - 1; i >= 0; i--) {
//				// 判断歌曲路径是否重复
//				if (players.get(i).getMusicPath().equals( path)) {
//					return;
//				}
//			}
            if (kz2) {
                File file = new File(path);
                if (file.exists()) {
                    MusicPlayer_3 yinxiao = new MusicPlayer_3(path);
                    yinxiao.start(false);
                    players.add(yinxiao);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            beijing(false);
            yinxiao(false);
            e.printStackTrace();
        }

    }
    /**
     * 背景控制
     *
     * @param
     */
    public static void beijing(boolean b) {
        try {
            kz1 = b;
            if (player != null) {
                if (kz1) {
                    player.continues();
                } else {
                    player.stop();
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

    /**
     * 音效控制
     *
     * @param
     */
    public static void yinxiao(boolean b) {
        try {
            kz2 = b;
            for (int i = 0; i < players.size(); i++) {
                try {
                    players.get(i).stop();
                    players.get(i).finalize();
                } catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            players.clear();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            players.clear();
        }

    }
}
